package com.innovation.chengzhangriji.util;

import android.graphics.Bitmap;

import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXTextObject;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;

public class WXUtils {

    /**
     * 发送文字给朋友
     */
    public static void sendTextToWXSession(IWXAPI api, String uuid, String description, String text) {
        sendTextToWX(api, uuid, description, text, SendMessageToWX.Req.WXSceneSession);
    }

    /**
     * 发送文字到朋友圈
     */
    public static void sendTextToWXTimeline(IWXAPI api, String uuid, String description, String text) {
        sendTextToWX(api, uuid, description, text, SendMessageToWX.Req.WXSceneTimeline);
    }

    /**
     * 发送网址给朋友
     */
    public static void sendUrlToWXSession(IWXAPI api, String uuid, String title, String description, String url, Bitmap thumb) {
        sendUrlToWX(api, uuid, title, description, url, thumb, SendMessageToWX.Req.WXSceneSession);
    }

    /**
     * 发送网址到朋友圈
     */
    public static void sendUrlToWXTimeline(IWXAPI api, String uuid, String description, String url, Bitmap thumb) {
        sendUrlToWX(api, uuid, description, description, url, thumb, SendMessageToWX.Req.WXSceneTimeline);
    }

    private static void sendTextToWX(IWXAPI api, String uuid, String description, String text, int scene) {
        WXTextObject wxObject = new WXTextObject();
        wxObject.text = text;
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = wxObject;
        msg.description = description;
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = uuid;
        req.message = msg;
        req.scene = scene;
        api.sendReq(req);
    }

    private static void sendUrlToWX(IWXAPI api, String uuid, String title, String description, String url, Bitmap thumb, int scene) {
        WXWebpageObject wxObject = new WXWebpageObject();
        wxObject.webpageUrl = url;
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = wxObject;
        msg.title = title;
        msg.description = description;
        if (thumb != null) {
            msg.setThumbImage(thumb);
        }
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = uuid;
        req.message = msg;
        req.scene = scene;
        api.sendReq(req);
    }
}
